package com.kaifamiao.wendao.dao;

import com.kaifamiao.wendao.entity.Customer;
import com.kaifamiao.wendao.entity.Level;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;

public class LevelDao extends BaseDao<Level,Long>{

    private final static String CREATE="INSERT INTO t_levels (id) VALUES (?) ";
    private final static String FIND_ONE="SELECT * FROM t_levels WHERE id=?";
    private final static String UPDATE_ONE="UPDATE t_levels SET logins=?, topics=?,  explains=? ,levels=? ,buys=? WHERE id =?";


    private Level wrap(ResultSet rs)throws SQLException {
        Level level=new Level();
        level.setCid(rs.getLong("id"));
        level.setLogin_time(rs.getInt("logins"));
        level.setTopic_time(rs.getInt("topics"));
        level.setExplain_time(rs.getInt("explains"));
        level.setEmpirical(rs.getInt("levels"));
        level.setBuys(rs.getInt("buys"));
        return level;
    }

    @Override
    public boolean save(Level level) {
        return false;
    }

    @Override
    public boolean update(Level level) {
        Object []params ={level.getLogin_time(),level.getTopic_time(),level.getExplain_time(),level.getEmpirical(),level.getBuys(),level.getCid()};
        try{
            return runner.update(UPDATE_ONE ,params)==1;
        }catch (SQLException e){
            throw new RuntimeException("查找失败",e);
        }
    }

    @Override
    public boolean remove(Long id) {
        return false;
    }

    @Override
    public List<Level> findAll() {
        return null;
    }

    @Override
    public Level find(Long id) {
        ResultSetHandler<Level> rsHandler = rs -> {
            if( rs.next() ) {
                Level level = wrap(rs);
                return level ;
            }
            return null;
        };
        try{
            return runner.query(FIND_ONE,rsHandler,id);
        }catch (SQLException e){
            throw new RuntimeException("查找失败",e);
        }
    }

    public boolean create(Long id){
        try{
            return runner.update(CREATE,id)==1;
        }catch (SQLException e){
            throw new RuntimeException("创建失败",e);
        }
    }

}
